package impl

import (
	"context"
	"gitee.com/kordar/goadmin/admin/service"
	"gitee.com/kordar/goadmin/reader"
	"github.com/kordar/ginsys/resource"
	"github.com/kordar/ginsys/resp"
	"github.com/kordar/gocrud"
	gocrudgorm "github.com/kordar/gocrud-gorm"
	"gorm.io/gorm"
	"labelsys/dataset/form"
	"labelsys/dataset/model"
	"labelsys/dataset/vo"
)

type DatasetServiceImpl struct {
	*gocrud.CommonResourceService
	db          *gorm.DB
	dictService service.DictService
}

func NewDatasetService(db *gorm.DB, dictService service.DictService) *DatasetServiceImpl {
	return &DatasetServiceImpl{db: db, dictService: dictService}
}

func (service DatasetServiceImpl) ResourceName() string {
	return "dataset"
}

func (service DatasetServiceImpl) Configs(ctx context.Context) map[string]interface{} {
	locale := resp.GetLocale(ctx)
	return map[string]interface{}{
		"state": service.dictService.Options("configs.switch-state", locale),
		"type":  service.dictService.Options("dataset.type", locale),
		"scene": service.dictService.Options("dataset.scene", locale),
	}
}

func (service DatasetServiceImpl) SearchOne(body gocrud.SearchBody) gocrud.SearchOneVO {
	if body.Conditions == nil || len(body.Conditions) == 0 {
		return gocrud.NewSearchOneVO(map[string]string{})
	}
	searchBody := gocrudgorm.NewGormSearchBody(body)
	labelsysDataset := model.LabelsysDataset{}
	tx := searchBody.GormQuery(service.db, nil).First(&labelsysDataset)
	if tx.Error != nil {
		return gocrud.NewSearchOneVO(map[string]string{})
	}

	return gocrud.NewSearchOneVO(map[string]interface{}{
		"id":          labelsysDataset.ID,
		"name":        labelsysDataset.Name,
		"description": labelsysDataset.Description,
		"state":       labelsysDataset.State,
		"scene":       labelsysDataset.Scene,
		"type":        labelsysDataset.Type,
		"label_count": labelsysDataset.LabelCount,
		"img_count":   labelsysDataset.ImgCount,
		"bucket_id":   labelsysDataset.BucketID,
		"create_time": labelsysDataset.CreateTime.Format("2006-01-02 15:04:05"),
		"update_time": labelsysDataset.UpdateTime.Format("2006-01-02 15:04:05"),
	})
}

func (service DatasetServiceImpl) Search(body gocrud.SearchBody) gocrud.SearchVO {
	searchBody := gocrudgorm.NewGormSearchBody(body)
	db := searchBody.GormQuery(service.db, nil).Model(&model.LabelsysDataset{})
	queryReader := reader.NewQueryReader(db, int(body.Page), int(body.PageSize))
	var datasetVOS []*vo.DatasetVO
	return queryReader.SearchVO(&datasetVOS, nil)
}

func (service DatasetServiceImpl) Edit(body gocrud.EditorBody) error {
	return body.Updates(&model.LabelsysDataset{}, service.db, nil)
}

func (service DatasetServiceImpl) Create(body gocrud.FormBody) (interface{}, error) {
	return resource.Create(body, service.db, &form.DatasetForm{}, nil, nil)
}

func (service DatasetServiceImpl) Update(body gocrud.FormBody) (interface{}, error) {
	return resource.Updates(body, service.db, &form.DatasetForm{}, nil, func(v interface{}) error {
		return nil
	})
}

func (service DatasetServiceImpl) Remove(body gocrud.RemoveBody) error {
	return body.Delete(&model.LabelsysDataset{}, service.db, nil)
}
